<?php
/**
 * Created by PhpStorm.
 * User: Lin
 * Date: 2017/3/1 0001
 * Time: 下午 13:25
 */
Class GoodsAction extends CommonAction{

    //商品首页视图
    public function index(){
        $this->display();
    }

    //显示商品列表
    public function showList(){
        $order=$_POST['order']?$_POST['order'].' DESC':' id DESC';
        $this->goodsArr=M('goods')->where(array('del'=>0))->order($order)->select();
        $this->display();
    }

    //添加商品
    public function addGoods(){
        import('Class.Category',APP_PATH);
        $this->cate=Category::formatCateLevel(M('cate')->order('sort ASC')->select());
        $this->display();
    }

    //运行添加商品
    public function runAddGoods(){
        import('ORG.Net.UploadFile');
        $upload = new UploadFile(array('autoSub'=>true,'subType'=>'date'));// 实例化上传类
        $upload->maxSize  = 3145728 ;// 设置附件上传大小
        $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath =  './Public/Uploads/';// 设置附件上传目录
        if(!$upload->upload()) {// 上传错误提示错误信息
            $this->error($upload->getErrorMsg());
        }else{// 上传成功 获取上传文件信息
            $info = $upload->getUploadFileInfo();
        }
        foreach ($info as $v){
          if( $v['key']=='img' ){
              $_POST['img']=$v['savepath'].$v['savename'];
          }else{
              $_POST['images']=$_POST['images'].'^'.$v['savepath'].$v['savename'].'$';
          }
        }

        if (M('goods')->add($_POST)){
            $this->success('添加成功',U(GROUP_NAME.'/Goods/showList'));
        }else{
            $this->error('添加失败');
        }
    }

    //编辑商品视图
    public function editGoods(){
        import('Class.Category',APP_PATH);
        $this->goods=M('goods')->find($_GET['id']);
        $this->cate=Category::formatCateLevel(M('cate')->order('sort ASC')->select(),$_GET['id']);
        $this->display();
    }

    //运行编辑商品
    public function runEditGoods(){
        if (M('goods')->save($_POST)){
            $this->success('编辑成功',U(GROUP_NAME.'/Goods/showList'));
        }else{
            $this->error('编辑失败');
        }
    }

    //编辑商品图片
    public function editPicture(){
        $this->goods=$_GET;
        $this->display();
    }

    //运行编辑商品图片
    public function runEditPicture(){
//        p($_POST,1);
        import('ORG.Net.UploadFile');
        $upload = new UploadFile(array('autoSub'=>true,'subType'=>'date'));// 实例化上传类
        $upload->maxSize  = 3145728 ;// 设置附件上传大小
        $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath =  './Public/Uploads/';// 设置附件上传目录
        if(!$upload->upload()) {// 上传错误提示错误信息
            $this->error($upload->getErrorMsg());
        }else{// 上传成功 获取上传文件信息
            $info = $upload->getUploadFileInfo();
        }
        foreach ($info as $v){
            if( $v['key']=='img' ){
                $_POST['img']=$v['savepath'].$v['savename'];
            }else{
                $_POST['images']=$_POST['images'].'src="'.$v['savepath'].$v['savename'].'"';
            }
        }
        if (M('goods')->save($_POST)){
            $this->success('编辑成功',U(GROUP_NAME.'/Goods/showList'));
        }else{
            $this->error('编辑失败');
        }
    }

    //运行删除或还原商品
    Public function toTrach(){
        $type=(int)$_GET['type'];
        $msg=$type?'删除':'还原';
        $updata=array(
            'id'=>I('id','',intval),
            'del'=>$type
        );
        if(M('goods')->save($updata)){
            $this->success($msg.'成功',U(GROUP_NAME.'/Goods/showList'));
        }else{
            $this->error($msg.'失败',U(GROUP_NAME.'/Goods/showList'));
        }
    }

    //显示回收站
    Public function trach(){
        $this->goodsArr=M('goods')->where(array('del'=>1))->order('sort')->select();
        $this->display();
    }

    //彻底删除商品
    Public function delete(){
        $id=(int)$_GET['id'];
        if(M('goods')->delete($id)){
            $this->success('彻底删除成功',U(GROUP_NAME.'/Goods/trach'));
        }
    }

    //AJAX请求运行编辑保存
    public function runEdit(){
        if(M('goods')->save($_POST)){
            echo true;
        }else{
            echo false;
        }
    }

    //ajax运行提交审核
    public function runCheck(){
        $data=array(
            'id'=>$_POST['id'],
            'status'=>$_POST['status'],
        );
        if(M('goods')->save($data)){
            echo true;
        }else{
            echo false;
        }
    }
}